package com.niit.session20;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StudentDao {
	public static void main(String[] args) {
		Connection conn = JDBCUtils.getConn();
		Statement statement = null;
		ResultSet rs = null;
		try {
			statement = conn.createStatement();
//			故意写错表名，让JDBC抛出SQLException
			rs = statement.executeQuery("select * from  students where batch = 2");
			while(rs.next()) {
				System.out.println(rs.getString("sid")+"--"+rs.getString("name"));
			}
		} catch (SQLException e) {
//			如果不想打印红色的堆栈信息，可以注释掉这行代码
//			e.printStackTrace();
//			获取错误消息
			System.out.println("错误消息："+e.getMessage());
//			获取SQL状态
			System.out.println("SQL状态："+e.getSQLState());
//			获取错误码
			System.out.println("错误码："+e.getErrorCode());
		}finally {
//			使用完毕之后记得关闭链接
			JDBCUtils.close(conn, statement, rs);
		}
	}

}
